//------------------- general

function $q(what,els){
    return what?what:typeof els!='undefined'?els:null;
}

function $addClass(where, what, remove) {
    cls = where.className.split(' ');
    cls.each(function(item,index) {
        if(item==what || item==remove) {
            delete cls[index];
        }
    });
    cls.push(what);
    where.className = cls.join(' ');
}

//------------------- w-specific

function WWSettings() {
    this.historyEnabled = false;
    if (window.historyStorage)
        this.historyEnabled = true;
    this.callbacks = { 
             onRefresh: null
    }
}

function initSettings(settings) {
    if (typeof settings !== 'undefined') {
        window.ww = settings;
        return;
    }
    if (typeof window.ww === 'undefined') {
        window.ww = new WWSettings();
        if (ww.historyEnabled)
            historyStorage.loadSettings();
    }
}

if (window.historyStorage) {
    Object.extend(window.historyStorage, {
        special: new Object(),
        getLastSavedAction: function() {
            return historyStorage.get('__currentHistoryAction');
        },
        setLastSavedAction: function(action) {
            historyStorage.put('__currentHistoryAction',action);
        },
        loadSettings: function() {
            saved = historyStorage.get('__currentWWSettings');
            if (saved)
                ww = saved;
        },
        saveSettings: function() {
            historyStorage.put('__currentWWSettings',window.ww);
        }
    });
    Object.extend(window.dhtmlHistory, {
        powerAdd: function(item) {
            if(typeof item.action === 'string') {
                var cha = historyStorage.getLastSavedAction();
                if(cha && item.unloadData.ID)
                    historyStorage.get(cha).unloadData = item.unloadData;
                item.unloadData = null;
                dhtmlHistory.add(item.action, item);
            }
            historyStorage.setLastSavedAction(item.action);
        }
    });
}

function historyChange(newLocation, 
                       historyData) {
    function fill(who,what) {
        if (typeof who === 'object' && who)
            if (typeof what === 'object')
                for(i=0;i<who.length;i=i+1)
                    $(who[i]).innerHTML = what[i];
            else
                for(i=0;i<who.length;i=i+1)
                    $(who[i]).innerHTML = what;
        else if(who)
            $(who).innerHTML = what;
    }
    initSettings();
    if (historyStorage.getLastSavedAction() == newLocation) {
        if (ww.callbacks.onRefresh) {
            ww.callbacks.onRefresh.call(null,newLocation);
            return;
        }
        window.location.href = '';
    }
    else
        historyStorage.setLastSavedAction(newLocation);

    hd = historyStorage.get(newLocation);
    if (newLocation == 'start')
        fill(hd.unloadData.ID,hd.unloadData.html);
    else if (newLocation) {
        if(hd.loadData)
            fill(hd.loadData.ID,hd.loadData.html);
        if(hd.unloadData)
            fill(hd.unloadData.ID,hd.unloadData.html);
    }
}

function initialize() {
    if (typeof window.preWWInit !== 'undefined')
        window.preWWInit();
    var defaulLoc = 'start';
    initSettings();
    if (!ww.historyEnabled)
        return;
    dhtmlHistory.initialize();
    dhtmlHistory.addListener(historyChange);
    if (!historyStorage.hasKey('__currentHistoryAction'))
        historyStorage.setLastSavedAction(defaulLoc);
    if (dhtmlHistory.isFirstLoad())
        dhtmlHistory.add(defaulLoc,new HistoryItem(defaulLoc,null,null));
}

function deinitialize() {
    if (ww.historyEnabled)
        historyStorage.saveSettings();
}

function HistoryData(ID,html) {
    this.ID = ID;
    this.html = html;
}

function HistoryItem(action,lData,uData) {
    this.action = action;
    this.loadData = lData;
    this.unloadData = uData;
}

window.onload = initialize;
window.onunload = deinitialize;
